package com.atguigu.gmall.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.client.service.ProductFeignClientService;
import com.atguigu.gmall.client.service.SearchFeignClientService;
import com.atguigu.gmall.entity.BaseCategoryView;
import com.atguigu.gmall.entity.ProductSalePropertyKey;
import com.atguigu.gmall.entity.SkuInfo;
import com.atguigu.gmall.executor.MyExecutor;
import com.atguigu.gmall.search.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Controller
@RequestMapping()
public class WebSkuDetailController {

    @Autowired
    private ProductFeignClientService productFeignClientService;
    @Autowired
    private SearchFeignClientService searchFeignClientService;

    //编写访问的控制器！
    //@RequestMapping("{skuId}.html")
    public String getSkuDetail01(@PathVariable Long skuId, Model model){
        //根据skuId 获取sku属性,存放在request中
        SkuInfo skuInfo = productFeignClientService.getSkuInfo(skuId);
        //获取分类信息
        BaseCategoryView categoryView = productFeignClientService.getCategoryView(skuInfo.getCategory3Id());
        //查询销售属性的key和value
        List<ProductSalePropertyKey> list = productFeignClientService.getSkuSalePropertyKeyAndValueList(skuInfo.getProductId(), skuId);
        //查询所有
        Map jsonMap = productFeignClientService.getSkuSalePropertyValueId(skuInfo.getProductId());

        Map<String, Object> map = new HashMap<>();

//        model.addAttribute("skuInfo",skuInfo);
        map.put("skuInfo", skuInfo);
        map.put("categoryView",categoryView);
        map.put("price", skuInfo.getPrice());
        map.put("skuSalePropertyKeyAndValueList", list); // key and value
        map.put("salePropertyValueIdJson", JSON.toJSONString(jsonMap));
        model.addAllAttributes(map);
        return "detail/index";
    }

    //使用异步编排的方式：使用多线程同时执行多个任务
    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model) {
        Map<String, Object> map = new HashMap<>();
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //根据skuId 获取sku属性,存放在request中
            SkuInfo skuInfo = productFeignClientService.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            map.put("price", skuInfo.getPrice());
            return skuInfo;
        }, MyExecutor.getInstance());
        //获取分类信息
        CompletableFuture<Void> voidCompletableFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClientService.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView",categoryView);
        }, MyExecutor.getInstance());
        //获取商品销售属性的key和value
        CompletableFuture<Void> skuSalePropertyKeyAndValueList = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            List<ProductSalePropertyKey> list = productFeignClientService.getSkuSalePropertyKeyAndValueList(skuInfo.getProductId(), skuId);
            map.put("skuSalePropertyKeyAndValueList", list); // key and value
        }, MyExecutor.getInstance());

        CompletableFuture<Void> salePropertyValueIdJson = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            Map jsonMap = productFeignClientService.getSkuSalePropertyValueId(skuInfo.getProductId());
            map.put("salePropertyValueIdJson", JSON.toJSONString(jsonMap));
        }, MyExecutor.getInstance());
        //更新商品热度
        CompletableFuture<Void> hotScoreFuture = CompletableFuture.runAsync(() -> {
            searchFeignClientService.incrHotScore(skuId);
        },MyExecutor.getInstance());
        //全部完成，才进行下一步  --循环栅栏juc
        CompletableFuture.allOf(
                skuInfoFuture,
                voidCompletableFuture,
                skuSalePropertyKeyAndValueList,
                salePropertyValueIdJson,
                hotScoreFuture
        ).join();

        model.addAllAttributes(map);
        return "detail/index";
    }


}
